//
//  SetShareUserAdminViewController.m
//  SenMo
//
//  Created by ZengYuan on 15/12/29.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "SetShareUserAdminViewController.h"
#import "DeviceHandle.h"

@interface SetShareUserAdminViewController ()

@end

@implementation SetShareUserAdminViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLabel.text = @"移交管理员";
    [self.rightBtnPar setTitle:@"确定" forState:UIControlStateNormal];
}

-(void)navRightAction{
    
    if(!targetUser){
        return;
    }
    
    NSString *atitle = [NSString stringWithFormat:@"您确定要把管理权移交给%@吗",targetUser.userName?targetUser.userName:targetUser.useruuid];
    
    UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:atitle message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"取消", @"确定",nil];
    [alertview show];
    alertview.tag=300;
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 300 && buttonIndex==1) {
        
        [DeviceHandle deviceAdminTransfer:self.deviceunit.device_id userid:targetUser.useruuid success:^{
            
            MBALERT(@"移交成功");
            
            [[NSNotificationCenter defaultCenter]
                postNotificationName:KHOME_FRESH_DEVICELIST_NOTI
                object:nil];
            
            [self.navigationController popToRootViewControllerAnimated:YES];
        } failure:^(NSString *error) {
            MBALERT(error);
        }];
     }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    for(UserUnit *user in self.allArray){
        user.selected = NO;
    }
    
    UserUnit *user = self.allArray[indexPath.row];
    user.selected = YES;
  
    targetUser = user;
    
    [self.tableView reloadData];
//    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
